package com.heima.xxl;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class HelloJob {
    @Value("${server.port}")
    private String port;

    @XxlJob("shardingJobHandle")
    public void shardingJobHandle(){
        int shardIndex = XxlJobHelper.getShardIndex();//获取当前分片的索引 执行器集群列表中当前执行器的序号
        int shardTotal = XxlJobHelper.getShardTotal();//总分片数 执行器集群的总机器数量
        List<Integer> list = getList();
        for (Integer integer : list) {
            if (integer % shardTotal == shardIndex) {
                log.info("当前第" + shardIndex + "分片执行了 +( "+ port + ")，任务项为：" + integer);
            }
        }
    }

    private List<Integer> getList(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
           list.add(i);
        }
        return list;
    }
}
